package com.example.panda.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.panda.Item.HomeTravelItem;
import com.example.panda.R;

import java.util.ArrayList;
import java.util.List;

public class HomeTravelRecipeAdapter extends RecyclerView.Adapter<HomeTravelRecipeAdapter.ViewHolder> {
    List<HomeTravelItem> list = new ArrayList<>();
    private Context context;
    private OnItemClickListener mOnItemClickListener;
    public HomeTravelRecipeAdapter(List<HomeTravelItem> list, Context context) {
        this.list = list;
        this.context = context;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.home_travel_recycle_item,parent,false);
        ViewHolder viewHolder= new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder( final ViewHolder holder, final int position) {
        HomeTravelItem item = list.get(position);
        holder.imageView.setImageResource(item.getImg());
        holder.textView.setText(item.getText());
        holder.titleView.setText(item.getTitle());
        if(mOnItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getAdapterPosition();
                    mOnItemClickListener.OnItemClick(v,pos);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getAdapterPosition();
                    mOnItemClickListener.OnItemLongClickListener(v,pos);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        Log.d("hyc",list.size()+"");
        return list.size();
    }

    public static interface OnItemClickListener{
        void OnItemClick(View view,int position);
        void OnItemLongClickListener(View view,int position);
    }
    public void setmOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener = onItemClickListener;
    }
    class ViewHolder extends  RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView textView;
        private TextView titleView;


        public ViewHolder( View itemView) {
            super(itemView);
            imageView = (ImageView)itemView.findViewById(R.id.imageView);
            textView = (TextView)itemView.findViewById(R.id.textView);
            titleView = (TextView)itemView.findViewById(R.id.titleView);
        }
    }
}
